package com.smartleanx.module.hrm.service.esign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.module.hrm.dal.dataobject.esign.ContractOrder;
import com.smartleanx.module.hrm.dal.mysql.esign.ContractOrderMapper;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.esign.bo.ContractOrderSelectBo;
import com.smartleanx.module.hrm.service.esign.service.ContractOrderService;
import com.smartleanx.module.hrm.service.esign.vo.ContractOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2025-07-14
 */
@Service
public class ContractOrderServiceImpl implements ContractOrderService {


    @Autowired
    private ContractOrderMapper contractOrderMapper;

    @Override
    public PageInfo<Object> selectPage(ContractOrderSelectBo contractOrderSelectBo) {
        PageHelper.startPage(contractOrderSelectBo.getPageNum(), contractOrderSelectBo.getPageSize());
        List<ContractOrder> contractOrders = contractOrderMapper.selectList(new LambdaQueryWrapper<ContractOrder>() {{
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getOrderSn()), ContractOrder::getOrderSn, contractOrderSelectBo.getOrderSn());
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayType()), ContractOrder::getPayType, contractOrderSelectBo.getPayType());
            ge(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayStartTime()), ContractOrder::getPayTime, contractOrderSelectBo.getPayStartTime());
            le(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayEndTime()), ContractOrder::getPayTime, contractOrderSelectBo.getPayEndTime());
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getOrderStatus()), ContractOrder::getOrderStatus, contractOrderSelectBo.getOrderStatus());
            ge(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateTimeStart()), ContractOrder::getCreateTime, contractOrderSelectBo.getCreateTimeStart());
            le(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateTimeEnd()), ContractOrder::getCreateTime, contractOrderSelectBo.getCreateTimeEnd());
            like(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateName()), ContractOrder::getCreateByName, contractOrderSelectBo.getCreateName());
            orderByDesc(ContractOrder::getCreateTime);
        }});

        return new PageInfo<>(contractOrders);
    }

    @Override
    public List<ContractOrderVo> selectListExcel(ContractOrderSelectBo contractOrderSelectBo) {
        List<ContractOrderVo> contractOrderVos = new ArrayList<>();
        List<ContractOrder> contractOrders = contractOrderMapper.selectList(new LambdaQueryWrapper<ContractOrder>() {{
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getOrderSn()), ContractOrder::getOrderSn, contractOrderSelectBo.getOrderSn());
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayType()), ContractOrder::getPayType, contractOrderSelectBo.getPayType());
            ge(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayStartTime()), ContractOrder::getPayTime, contractOrderSelectBo.getPayStartTime());
            le(!ObjectUtils.isEmpty(contractOrderSelectBo.getPayEndTime()), ContractOrder::getPayTime, contractOrderSelectBo.getPayEndTime());
            eq(!ObjectUtils.isEmpty(contractOrderSelectBo.getOrderStatus()), ContractOrder::getOrderStatus, contractOrderSelectBo.getOrderStatus());
            ge(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateTimeStart()), ContractOrder::getCreateTime, contractOrderSelectBo.getCreateTimeStart());
            le(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateTimeEnd()), ContractOrder::getCreateTime, contractOrderSelectBo.getCreateTimeEnd());
            like(!ObjectUtils.isEmpty(contractOrderSelectBo.getCreateName()), ContractOrder::getCreateByName, contractOrderSelectBo.getCreateName());
            orderByDesc(ContractOrder::getCreateTime);
        }});
        contractOrders.forEach(contractOrder -> {
            if (ObjectUtils.isEmpty(contractOrder.getPayType())) {
                contractOrder.setPayTypeStr("--");
            } else {
                switch (contractOrder.getPayType()) {
                    case 1:
                        contractOrder.setPayTypeStr("微信");
                        break;
                    case 2:
                        contractOrder.setPayTypeStr("支付宝");
                        break;
                }
            }
//            1、待支付 2支付成功 3、支付失败 4、超时未支付 5、已关闭
            switch (contractOrder.getOrderStatus()) {
                case 1:
                    contractOrder.setOrderStatusStr("待支付");
                    break;
                case 2:
                    contractOrder.setOrderStatusStr("支付成功");
                    break;
                case 3:
                    contractOrder.setOrderStatusStr("支付失败");
                    break;
                case 4:
                    contractOrder.setOrderStatusStr("超时未支付");
                    break;
                case 5:
                    contractOrder.setOrderStatusStr("已关闭");
                    break;
            }
            ContractOrderVo contractOrderVo = new ContractOrderVo();
            BeanUtils.copyProperties(contractOrder, contractOrderVo);
            contractOrderVos.add(contractOrderVo);

        });
        return contractOrderVos;
    }
}
